package com.tongzhou.tongzhou.quot.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.tongzhou.db.DBUtil;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperTypeCoreDAO;
import com.tongzhou.tongzhou.quot.model.PaperCore;
import com.tongzhou.tongzhou.quot.model.PaperTypeCore;
import com.tongzhou.tongzhou.quot.model.SearchPaperTypeCoreCondition;
import com.tongzhou.util.Util;

public class PaperTypeCoreDAO implements IPaperTypeCoreDAO{
	
	@Override
	public int addPaperCore(PaperCore paperCore) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into paper_core(paper_core_name,price,paper_type_core) ");
		sql.append("values('"+Util.changeToObj(paperCore.getPaper_core_name())+"',");
		sql.append("'"+Util.changeToObj(paperCore.getPrice())+"',");
		sql.append("'"+Util.changeToObj(paperCore.getPaper_type_core())+"')");
		int result=DBUtil.update(sql.toString());
		return result;
	}

	@Override
	public void deletePaperTypeCore() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from paper_core ");
		DBUtil.update(sql.toString());
		DBUtil.update(sql.toString());
	}

	@Override
	public List<PaperTypeCore> listPaperTypeCore(SearchPaperTypeCoreCondition condition)
			throws DBException {
		List<PaperTypeCore> result=null;
		
		//查询条件SQL
		//查询参数
		StringBuffer sqlCondition = new StringBuffer() ;
		List<String> paramList = new ArrayList<String>() ;
		
		if(condition.getType() != null && !condition.getType().equals("")){
			sqlCondition.append(" and type =?");
			paramList.add("'"+condition.getType()+"'");
		}
		
		//取得满足条件的总记录数和总页数
		StringBuffer sql = new StringBuffer() ;
		sql.append("select count(id) ");
		sql.append(" from paper_type_core ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		
		//总记录数
		long total = DBUtil.getCount(sql.toString(),paramList.toArray()) ;
		//总页数
		long totalPage = Util.getTotalPage(total, condition.getPageSize());
		
		condition.setTotal(total);
		condition.setTotalPage(totalPage);
		
		//查询某页的列表数据
		sql.delete(0, sql.length());
		sql.append("select  id,type_core_name,type,create_time ");
		sql.append(" from paper_type_core ");
		sql.append(" where 1=1 ");
		sql.append(sqlCondition);
		sql.append(" order by "+condition.getOrderBy()+" "+condition.getOrderByType()) ;
		result = DBUtil.query(sql.toString(), new BeanListHandler<PaperTypeCore>(PaperTypeCore.class), paramList.toArray(), Util.getStartPos(condition.getCurrentPage(), condition.getPageSize()), condition.getPageSize()) ;
		return result;
	}

	@Override
	public void updatePaperTypeCore() throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("update paper_type_core set ");
		sql.append("create_time=now() ");
		DBUtil.update(sql.toString());
	}

	@Override
	public List<PaperCore> getPaperCoreList(String Type) throws DBException {
		StringBuffer sql=new StringBuffer();
		sql.append("select id,paper_core_name,price,paper_type_core from paper_core ");
		sql.append(" where 1=1 ");
		if(Type!=null && !Type.equals("")){
			sql.append(" and paper_type_core='"+Type+"' ");
		}
		sql.append(" order by id asc ");
		List<PaperCore> result=DBUtil.query(sql.toString(),new BeanListHandler<PaperCore>(PaperCore.class));
		return result;
	}
}
